<?php
class o2o_inventory_confirm {

    function run(&$cursor_id,$params,&$errmsg){
        //主订单数据
        $rs_inventory = $params["sdfdata"]["rs_inventory"];
        
        //全渠道库存接口
        if($rs_inventory["inventory_type"] == "1"){
            //全量
            $param = kernel::single('tbo2o_common_tbo2oapi')->getInventoryRequestParam($params["sdfdata"]["products"],$params["sdfdata"]["branch_id"]);
            $param["inventory_bn"] = $rs_inventory["inventory_bn"];
            $result = kernel::single('tbo2o_event_trigger_store')->storeinventoryIteminitial($param);
        }else{
            //增量
            $param = kernel::single('tbo2o_common_tbo2oapi')->getInventoryRequestParam($params["sdfdata"]["products"],$params["sdfdata"]["branch_id"],"update");
            $param["inventory_bn"] = $rs_inventory["inventory_bn"];
            $result = kernel::single('tbo2o_event_trigger_store')->storeinventoryItemupdate($param);
        }
        
        $data = json_decode($result["data"],true);
        if ($data["succ"][0]["response"]["flag"] != "success"){
            $errmsg = '门店盘点单确认失败';
        }else{
            //打库存接口成功
            $mdlO2oProductStore = app::get('o2o')->model('product_store');
            $mdlO2oInventory = app::get('o2o')->model('inventory');
            $current_time = time();
            foreach ($params["sdfdata"]["products"] as $var_s){
                $update_arr = array(
                        "store"=>$var_s["store"],
                        "share_store"=>$var_s["share_store"],
                        "last_modified"=>$current_time,
                );
                $filter_arr = array("id"=>$var_s["id"]);
                $mdlO2oProductStore->update($update_arr,$filter_arr);
            }
            
            //更新盘点主表状态为已确认
            $update_inventory_arr = array(
                    "confirm_op_id"=>$params["sdfdata"]["confirm_op_id"],
                    "confirm_time"=>$current_time,
                    "status"=>2,
            );
            $filter_inventory_arr = array("inventory_id"=>$rs_inventory["inventory_id"]);
            $mdlO2oInventory->update($update_inventory_arr,$filter_inventory_arr);
        }
        return false;
    }
}
